package com.hmy.finance.service.receiveAdjust.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmy.ccp.starter.web.utils.UserContextUtil;
import com.hmy.finance.bo.receiveAdjust.request.SaveReceiveAdjustRequest;
import com.hmy.finance.bo.receiveAdjust.response.QueryReceiveAdjustDetailResponse;
import com.hmy.finance.common.constants.DictionaryConstant;
import com.hmy.finance.dal.mapper.receiveAdjust.ReceiveAdjustDetailMapper;
import com.hmy.finance.dal.po.receiveAdjust.ReceiveAdjustDetailPo;
import com.hmy.finance.dal.po.receiveAdjust.ReceiveAdjustPo;
import com.hmy.finance.manager.uua.DataDictionaryClientRpc;
import com.hmy.finance.service.receiveAdjust.ReceiveAdjustDetailService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author wenjian
 * @description 表receive_adjust_detail(应收调整详情表)的数据库操作Service实现
 * @createDate 2024-02-26 10:16
 */
@Service
public class ReceiveAdjustDetailServiceImpl extends ServiceImpl<ReceiveAdjustDetailMapper, ReceiveAdjustDetailPo> implements ReceiveAdjustDetailService {

    @Resource
    private ReceiveAdjustDetailMapper receiveAdjustDetailMapper;

    @Resource
    private DataDictionaryClientRpc dataDictionary;

    /**
     * 删除调整详情
     *
     * @param adjustId 应收调整id
     */
    public void delete(Long adjustId) {
        ReceiveAdjustDetailPo rad = new ReceiveAdjustDetailPo();
        UserContextUtil.initUpdaterInfo(rad);
        rad.setIsDeleted(true);
        receiveAdjustDetailMapper.update(rad, Wrappers.lambdaUpdate(ReceiveAdjustDetailPo.class).
                eq(ReceiveAdjustDetailPo::getAdjustId, adjustId));
    }

    @Override
    @Transactional
    public void saveOrUpdate(ReceiveAdjustPo receiveAdjustPo, SaveReceiveAdjustRequest saveReceiveAdjustRequest) {
        if (CollectionUtils.isEmpty(saveReceiveAdjustRequest.getReceiveAdjustDetails())) {
            return;
        }
        // 先删除
        LambdaQueryWrapper<ReceiveAdjustDetailPo> queryWrapper = Wrappers.<ReceiveAdjustDetailPo>lambdaQuery()
                .eq(ReceiveAdjustDetailPo::getAdjustId, receiveAdjustPo.getAdjustId());
        this.remove(queryWrapper);

        // 再保存
        List<ReceiveAdjustDetailPo> list = new ArrayList<>();
        saveReceiveAdjustRequest.getReceiveAdjustDetails().forEach(rad -> {
            ReceiveAdjustDetailPo adjustDetail = new ReceiveAdjustDetailPo();
            BeanUtil.copyProperties(rad, adjustDetail);
            adjustDetail.setAdjustId(receiveAdjustPo.getAdjustId());
            adjustDetail.setAdjustNo(receiveAdjustPo.getAdjustNo());
            UserContextUtil.initCreatorInfo(adjustDetail);
            list.add(adjustDetail);
        });
        this.saveBatch(list);
    }

    @Override
    public List<QueryReceiveAdjustDetailResponse> queryReceiveAdjustDetails(Long adjustId) {
        List<QueryReceiveAdjustDetailResponse> list = new ArrayList<>();
        List<ReceiveAdjustDetailPo> receiveAdjustDetails =
                this.list(Wrappers.lambdaQuery(ReceiveAdjustDetailPo.class).eq(ReceiveAdjustDetailPo::getAdjustId,
                        adjustId).eq(ReceiveAdjustDetailPo::getIsDeleted, false));
        if (CollectionUtils.isEmpty(receiveAdjustDetails)) {
            return list;
        }
        Map<String, String> typeMap = dataDictionary.queryNodeLabel(DictionaryConstant.ADJUST_TYPE);
        Map<String, String> wayMap = dataDictionary.queryNodeLabel(DictionaryConstant.ADJUST_WAY);
        receiveAdjustDetails.forEach(de -> {
            QueryReceiveAdjustDetailResponse res = new QueryReceiveAdjustDetailResponse();
            BeanUtil.copyProperties(de, res);
            res.setAdjustTypeLabel(de.getAdjustType() == null ? null : typeMap.get(de.getAdjustType().toString()));
            res.setAdjustWayLabel(de.getAdjustWay() == null ? null : wayMap.get(de.getAdjustWay().toString()));
            list.add(res);
        });
        return list;
    }

}




